home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter6 / isohex6_4 / isohex6_4.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-22  |  11.5 KB  |  455 lines

  1. /*****************************************************************************
  2. IsoHex6_4.cpp
  3. Ernest S. Pazera
  4. 22MAY2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib and dxguid.lib
  7. Needs GDICanvas.h/cpp
  8. Needs DDFuncs.h/cpp
  9. *****************************************************************************/
  10.  
  11. //////////////////////////////////////////////////////////////////////////////
  12. //INCLUDES
  13. //////////////////////////////////////////////////////////////////////////////
  14. #define WIN32_LEAN_AND_MEAN  
  15.  
  16. #include <windows.h>   
  17. #include "GDICanvas.h"
  18. #include "ddraw.h"
  19. #include "DDFuncs.h"
  20.  
  21. //////////////////////////////////////////////////////////////////////////////
  22. //DEFINES
  23. //////////////////////////////////////////////////////////////////////////////
  24. //name for our window class
  25. #define WINDOWCLASS "ISOHEX6"
  26. //title of the application
  27. #define WINDOWTITLE "IsoHex 6-2"
  28.  
  29. //////////////////////////////////////////////////////////////////////////////
  30. //PROTOTYPES
  31. //////////////////////////////////////////////////////////////////////////////
  32. bool Prog_Init();//game data initalizer
  33. void Prog_Loop();//main game loop
  34. void Prog_Done();//game clean up
  35.  
  36. //enumeration functions
  37. HRESULT WINAPI EnumModesCallbackCount(LPDDSURFACEDESC2 lpDDSurfaceDesc, LPVOID lpContext);
  38. HRESULT WINAPI EnumModesCallbackList(LPDDSURFACEDESC2 lpDDSurfaceDesc,LPVOID lpContext);
  39.  
  40. //////////////////////////////////////////////////////////////////////////////
  41. //GLOBALS
  42. //////////////////////////////////////////////////////////////////////////////
  43. HINSTANCE hInstMain=NULL;//main application handle
  44. HWND hWndMain=NULL;//handle to our main window
  45. //IDirectDraw7 Pointer
  46. LPDIRECTDRAW7 lpdd=NULL;
  47. //display mode structure
  48. struct DisplayMode
  49. {
  50.     DWORD dwWidth;
  51.     DWORD dwHeight;
  52.     DWORD dwBPP;
  53. };
  54. //display mode enumeration variables
  55. DWORD dwDisplayModeCount=0;
  56. DisplayMode* DisplayModeList=NULL;
  57.  
  58. //gdicanvas
  59. CGDICanvas gdicBall;
  60.  
  61. //surfaces
  62. LPDIRECTDRAWSURFACE7 lpddsPrime=NULL;
  63. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  64. LPDIRECTDRAWSURFACE7 lpddsBall=NULL;
  65.  
  66. //size of the display
  67. DWORD dwDisplayWidth=0;
  68. DWORD dwDisplayHeight=0;
  69.  
  70. //position of the ball
  71. POINT ptBallPosition[2];
  72. POINT ptLastPosition[2][2];
  73. //velocity of the ball
  74. POINT ptBallVelocity[2];
  75.  
  76. //paused mode
  77. bool bPaused=false;
  78.  
  79. //////////////////////////////////////////////////////////////////////////////
  80. //WINDOWPROC
  81. //////////////////////////////////////////////////////////////////////////////
  82. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  83. {
  84.     //which message did we get?
  85.     switch(uMsg)
  86.     {
  87.     case WM_ACTIVATEAPP:
  88.         {
  89.             if(wParam)
  90.             {
  91.                 if(bPaused)
  92.                 {
  93.                     //being activated
  94.                     bPaused=false;
  95.  
  96.                     //restore all surfaces
  97.                     lpdd->RestoreAllSurfaces();
  98.  
  99.                     //clear out back buffer
  100.                     DDBLTFX ddbltfx;
  101.                     DDBLTFX_ColorFill(&ddbltfx,0);
  102.                     lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  103.  
  104.                     //reload ball images
  105.                     LPDDS_ReloadFromFile(lpddsBall,"IsoHex6_4.bmp");
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 //being deactivated
  111.                 bPaused=true;
  112.             }
  113.         }break;
  114.     case WM_KEYDOWN:
  115.         {
  116.             //check for escape key
  117.             if(wParam==VK_ESCAPE)
  118.             {
  119.                 DestroyWindow(hWndMain);
  120.             }
  121.  
  122.             return(0);//handled message
  123.         }break;
  124.     case WM_DESTROY://the window is being destroyed
  125.         {
  126.  
  127.             //tell the application we are quitting
  128.             PostQuitMessage(0);
  129.  
  130.             //handled message, so return 0
  131.             return(0);
  132.  
  133.         }break;
  134.     case WM_PAINT://the window needs repainting
  135.         {
  136.             //a variable needed for painting information
  137.             PAINTSTRUCT ps;
  138.             
  139.             //start painting
  140.             HDC hdc=BeginPaint(hwnd,&ps);
  141.  
  142.             /////////////////////////////
  143.             //painting code would go here
  144.             /////////////////////////////
  145.  
  146.             //end painting
  147.             EndPaint(hwnd,&ps);
  148.                         
  149.             //handled message, so return 0
  150.             return(0);
  151.         }break;
  152.     }
  153.  
  154.     //pass along any other message to default message handler
  155.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  156. }
  157.  
  158.  
  159. //////////////////////////////////////////////////////////////////////////////
  160. //WINMAIN
  161. //////////////////////////////////////////////////////////////////////////////
  162. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  163. {
  164.     //assign instance to global variable
  165.     hInstMain=hInstance;
  166.  
  167.     //create window class
  168.     WNDCLASSEX wcx;
  169.  
  170.     //set the size of the structure
  171.     wcx.cbSize=sizeof(WNDCLASSEX);
  172.  
  173.     //class style
  174.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  175.  
  176.     //window procedure
  177.     wcx.lpfnWndProc=TheWindowProc;
  178.  
  179.     //class extra
  180.     wcx.cbClsExtra=0;
  181.  
  182.     //window extra
  183.     wcx.cbWndExtra=0;
  184.  
  185.     //application handle
  186.     wcx.hInstance=hInstMain;
  187.  
  188.     //icon
  189.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  190.  
  191.     //cursor
  192.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  193.  
  194.     //background color
  195.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  196.  
  197.     //menu
  198.     wcx.lpszMenuName=NULL;
  199.  
  200.     //class name
  201.     wcx.lpszClassName=WINDOWCLASS;
  202.  
  203.     //small icon
  204.     wcx.hIconSm=NULL;
  205.  
  206.     //register the window class, return 0 if not successful
  207.     if(!RegisterClassEx(&wcx)) return(0);
  208.  
  209.     //create main window
  210.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  211.  
  212.     //error check
  213.     if(!hWndMain) return(0);
  214.  
  215.     //if program initialization failed, then return with 0
  216.     if(!Prog_Init()) return(0);
  217.  
  218.     //message structure
  219.     MSG msg;
  220.  
  221.     //message pump
  222.     for(;;)    
  223.     {
  224.         //look for a message
  225.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  226.         {
  227.             //there is a message
  228.  
  229.             //check that we arent quitting
  230.             if(msg.message==WM_QUIT) break;
  231.             
  232.             //translate message
  233.             TranslateMessage(&msg);
  234.  
  235.             //dispatch message
  236.             DispatchMessage(&msg);
  237.         }
  238.  
  239.         //run main game loop
  240.         Prog_Loop();
  241.     }
  242.     
  243.     //clean up program data
  244.     Prog_Done();
  245.  
  246.     //return the wparam from the WM_QUIT message
  247.     return(msg.wParam);
  248. }
  249.  
  250. //////////////////////////////////////////////////////////////////////////////
  251. //INITIALIZATION
  252. //////////////////////////////////////////////////////////////////////////////
  253. bool Prog_Init()
  254. {
  255.     lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
  256.  
  257.     //enumerate the displaymodes
  258.     dwDisplayModeCount=0;
  259.  
  260.     lpdd->EnumDisplayModes(0,NULL,NULL,EnumModesCallbackCount);
  261.  
  262.     DisplayModeList=new DisplayMode[dwDisplayModeCount];
  263.     dwDisplayModeCount=0;
  264.  
  265.     lpdd->EnumDisplayModes(0,NULL,NULL,EnumModesCallbackList);
  266.  
  267.     //pick a display mode
  268.     DisplayMode TestMode;
  269.     TestMode.dwWidth=0;
  270.     TestMode.dwHeight=0;
  271.     TestMode.dwBPP=0;
  272.     DWORD index;
  273.     bool found=false;
  274.  
  275.     for(index=0;(index<dwDisplayModeCount);index++)
  276.     {
  277.         if(DisplayModeList[index].dwBPP==16)
  278.         {
  279.             if(DisplayModeList[index].dwWidth>TestMode.dwWidth)
  280.             {
  281.                 TestMode.dwWidth=DisplayModeList[index].dwWidth;
  282.                 TestMode.dwHeight=DisplayModeList[index].dwHeight;
  283.                 TestMode.dwBPP=DisplayModeList[index].dwBPP;
  284.                 found=true;
  285.             }
  286.         }
  287.     }
  288.  
  289.     if(!found)
  290.     {
  291.         return(false);
  292.     }
  293.  
  294.     //set the display mode
  295.     lpdd->SetDisplayMode(TestMode.dwWidth,TestMode.dwHeight,TestMode.dwBPP,0,0);
  296.     //keep display width and height in global variables
  297.     dwDisplayWidth=TestMode.dwWidth;
  298.     dwDisplayHeight=TestMode.dwHeight;
  299.  
  300.     //create the primary surface with a single back buffer
  301.     lpddsPrime=LPDDS_CreatePrimary(lpdd,1);
  302.  
  303.     //retrieve back buffer
  304.     lpddsBack=LPDDS_GetSecondary(lpddsPrime);
  305.  
  306.     //do an initial clearing out of the back buffer
  307.     DDBLTFX ddbltfx;
  308.     DDBLTFX_ColorFill(&ddbltfx,0);
  309.  
  310.     //do a color fill
  311.     lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL, &ddbltfx);
  312.  
  313.     //load in the ball image
  314.     gdicBall.Load(NULL,"IsoHex6_4.bmp");
  315.  
  316.     //create an offscreen surface to contain the ball
  317.     lpddsBall=LPDDS_LoadFromFile(lpdd,"IsoHex6_4.bmp");
  318.  
  319.     //create a black color key
  320.     LPDDS_SetSrcColorKey(lpddsBall,0);
  321.  
  322.     //initialize ball position and velocity
  323.     ptBallPosition[0].x=0;
  324.     ptBallPosition[0].y=0;
  325.     ptLastPosition[0][0].x=0;
  326.     ptLastPosition[0][0].y=0;
  327.     ptLastPosition[0][1].x=0;
  328.     ptLastPosition[0][1].y=0;
  329.     ptBallPosition[1].x=0;
  330.     ptBallPosition[1].y=0;
  331.     ptLastPosition[1][0].x=0;
  332.     ptLastPosition[1][0].y=0;
  333.     ptLastPosition[1][1].x=0;
  334.     ptLastPosition[1][1].y=0;
  335.  
  336.     ptBallVelocity[0].x=4;
  337.     ptBallVelocity[0].y=2;
  338.     ptBallVelocity[1].x=2;
  339.     ptBallVelocity[1].y=4;
  340.  
  341.     return(true);//return success
  342. }
  343.  
  344. //////////////////////////////////////////////////////////////////////////////
  345. //CLEANUP
  346. //////////////////////////////////////////////////////////////////////////////
  347. void Prog_Done()
  348. {    
  349.     //clean up ball surface
  350.     LPDDS_Release(&lpddsBall);
  351.         
  352.     //clean up primary surface(this will clean up the back buffer, also)
  353.     LPDDS_Release(&lpddsPrime);
  354.  
  355.     //clean up the dd pointer
  356.     LPDD_Release(&lpdd);
  357.  
  358.     //clean up gdicanvas
  359.     gdicBall.Destroy();
  360.  
  361.     //get rid of enumeration stuff
  362.     delete [] DisplayModeList;
  363. }
  364.  
  365. //////////////////////////////////////////////////////////////////////////////
  366. //MAIN GAME LOOP
  367. //////////////////////////////////////////////////////////////////////////////
  368. void Prog_Loop()
  369. {
  370.     //if paused, return without doing anything
  371.     if(bPaused) return;
  372.  
  373.     int index;
  374.  
  375.     for(index=0;index<2;index++)
  376.     {
  377.  
  378.     //set up rectangle for filling
  379.     RECT rcFill;
  380.     SetRect(&rcFill,ptLastPosition[index][0].x,ptLastPosition[index][0].y,ptLastPosition[index][0].x+gdicBall.GetWidth(),ptLastPosition[index][0].y+gdicBall.GetHeight());
  381.  
  382.     //set up a ddbltfx
  383.     DDBLTFX ddbltfx;
  384.     DDBLTFX_ColorFill(&ddbltfx,0);
  385.  
  386.     //blt the color fill to the back buffer
  387.     lpddsBack->Blt(&rcFill,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL, &ddbltfx);
  388.     }
  389.  
  390.     for(index=0;index<2;index++)
  391.     {
  392.     //set up source and destination rects for blitting the ball
  393.     RECT rcSrc;
  394.     RECT rcDst;
  395.  
  396.     SetRect(&rcSrc,0,0,gdicBall.GetWidth(),gdicBall.GetHeight());
  397.     CopyRect(&rcDst,&rcSrc);
  398.     OffsetRect(&rcDst,ptBallPosition[index].x,ptBallPosition[index].y);
  399.  
  400.     //blit the ball
  401.     lpddsBack->Blt(&rcDst,lpddsBall,&rcSrc,DDBLT_WAIT | DDBLT_KEYSRC, NULL);
  402.  
  403.     //copy current position of ball to old position
  404.     ptLastPosition[index][0]=ptLastPosition[index][1];
  405.     ptLastPosition[index][1]=ptBallPosition[index];
  406.  
  407.     //move the ball
  408.     ptBallPosition[index].x+=ptBallVelocity[index].x;
  409.     ptBallPosition[index].y+=ptBallVelocity[index].y;
  410.  
  411.     //bounds checking
  412.     //left side
  413.     if(ptBallPosition[index].x<=0) ptBallVelocity[index].x=abs(ptBallVelocity[index].x);
  414.     //top side
  415.     if(ptBallPosition[index].y<=0) ptBallVelocity[index].y=abs(ptBallVelocity[index].y);
  416.     //right side
  417.     if(ptBallPosition[index].x>=(int)dwDisplayWidth-gdicBall.GetWidth()) ptBallVelocity[index].x=-abs(ptBallVelocity[index].x);
  418.     //bottom side
  419.     if(ptBallPosition[index].y>=(int)dwDisplayHeight-gdicBall.GetHeight()) ptBallVelocity[index].y=-abs(ptBallVelocity[index].y);
  420.     }
  421.     //flip surfaces
  422.     lpddsPrime->Flip(NULL,DDFLIP_WAIT);
  423. }
  424.  
  425. //enumeration-count
  426. HRESULT WINAPI EnumModesCallbackCount(
  427.   LPDDSURFACEDESC2 lpDDSurfaceDesc,  
  428.   LPVOID lpContext                   
  429. )
  430. {
  431.     //increment the count variable
  432.     dwDisplayModeCount++;
  433.  
  434.     //continue the enumeration
  435.     return(DDENUMRET_OK);
  436. }
  437.  
  438. //enumeration-list
  439. HRESULT WINAPI EnumModesCallbackList(
  440.   LPDDSURFACEDESC2 lpDDSurfaceDesc,  
  441.   LPVOID lpContext                   
  442. )
  443. {
  444.     //copy applicable information to the list
  445.     DisplayModeList[dwDisplayModeCount].dwWidth=lpDDSurfaceDesc->dwWidth;
  446.     DisplayModeList[dwDisplayModeCount].dwHeight=lpDDSurfaceDesc->dwHeight;
  447.     DisplayModeList[dwDisplayModeCount].dwBPP=lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount;
  448.  
  449.     //increment the count variable
  450.     dwDisplayModeCount++;
  451.  
  452.     //continue the enumeration
  453.     return(DDENUMRET_OK);
  454. }
  455.